<?php

/**
 * Description of ContattiController
 *
 * @author luigi.pastore
 * @created  16-mag-2011
 */
class ContattiController extends Classes_Controller
{
    public function init()
    {
	$this->model_contact	= new Classes_Contact();
	$this->model_province	= new Classes_Province();
	
	$this->contact_roles = $this->application->getResource( 'ContactRoles' );
	
	$this->layout->province = $this->model_province->fetchAll();
	$this->layout->contact_roles = $this->contact_roles;
	
    }
    
    public function indexAction()
    {
	$this->layout->content = array( 'script' => 'artisti/index' );
    }
    
    public function nuovoAction()
    {
	if( $_POST['salva'] AND $_POST['salva'] !== '' )
	{
	    $this->layout->data = $_POST;
	    
	    // Validiamo server-side...
	    $errorMsg = $this->checkFormData( $_POST );

	    if( !$errorMsg )
	    {
		// Mapping...
		$data = array(
		    'contact_firstname'		=> ucfirst( $_POST['contact_firstname'] ),
		    'contact_lastname'		=> ucfirst( $_POST['contact_lastname'] ),
		    'contact_email'		=> $_POST['contact_email'],
		    'contact_phone'		=> $_POST['contact_phone_prefix'] .'/'. $_POST['contact_phone_number'],
		    'contact_email2'		=> $_POST['contact_email2'],
		    'contact_email3'		=> $_POST['contact_email3'],
		    'contact_work'		=> $_POST['contact_work_prefix'] .'/'. $_POST['contact_work_number'],
		    'contact_mobile'		=> $_POST['contact_mobile_prefix'] .'/'. $_POST['contact_mobile_number'],
		    'contact_fax'		=> $_POST['contact_fax_prefix'] .'/'. $_POST['contact_fax_number'],
		    'contact_city'		=> $_POST['contact_city'],
		    'contact_cap'		=> $_POST['contact_cap'],
		    'contact_loc'		=> $_POST['contact_loc'],
		    'contact_street'		=> $_POST['contact_street'],
		    'contact_street_number'	=> $_POST['contact_street_number'],
		    'contact_province_id'	=> $_POST['contact_province_id'],		    
		    'contact_notes'		=> $_POST['contact_notes'],		    
		    'contact_createdby'		=> $this->application->auth->user['user_id']
		);
		
		$model = new Classes_Contact( $data );

		if( is_numeric( $contact_id = $model->save()->contact_id ) )
		{
		    // Rimanda alla pagina di conferma e cancella file temporanei...
		    $this->layout->forward = 'contatti/rubrica';

		    return $this->layout->content = array( 'script' => 'completed' );
		}
		else
		{
		    trigger_error( 'Errore Inserimento Contatto' );
		}
	    }
	}

	$this->layout->errorMsg = $errorMsg;
	$this->layout->content = array( 'script' => 'contatti/nuovo' );
    }

    public function editAction()
    {
	// Edit
	if( !is_null( $this->getParam( 'id' ) ) )
	{
	    // Fetch
	    $contact = $this->model_contact->fetchById( array( $this->getParam( 'id' ) ) );
  
	    // Se non c'è il post lancia l'errore
	    if( !is_array( $contact ) )
	    {
		trigger_error( 'Errore Edit Contatto', E_USER_WARNING );
	    }
	    else
	    {
		$contact = array_shift( $contact );
		
		// Format..
		list( $contact['contact_phone_prefix'], $contact['contact_phone_number'] ) = explode( '/', $contact['contact_phone'] );
		list( $contact['contact_work_prefix'], $contact['contact_work_number'] ) = explode( '/', $contact['contact_work'] );
		list( $contact['contact_mobile_prefix'], $contact['contact_mobile_number'] ) = explode( '/', $contact['contact_mobile'] );
		list( $contact['contact_fax_prefix'], $contact['contact_fax_number'] ) = explode( '/', $contact['contact_fax'] );
		
		# Assegniamo i dati del post al layout
		$this->layout->data = $contact;
	    }
	}
	
	if( $_POST['salva'] AND $_POST['salva'] !== '' )
	{
	    $this->layout->data = $_POST;
	    
	    // Validiamo server-side...
	    $errorMsg = $this->checkFormData( $_POST );

	    if( !$errorMsg )
	    {
		// Mapping...
		$data = array(
		    'contact_id'		=> $contact['contact_id'],
		    'contact_firstname'		=> $_POST['contact_firstname'],
		    'contact_lastname'		=> $_POST['contact_lastname'],
		    'contact_email'		=> $_POST['contact_email'],
		    'contact_phone'		=> $_POST['contact_phone_prefix'] .'/'. $_POST['contact_phone_number'],
		    'contact_email2'		=> $_POST['contact_email2'],
		    'contact_email3'		=> $_POST['contact_email3'],
		    'contact_work'		=> $_POST['contact_work_prefix'] .'/'. $_POST['contact_work_number'],
		    'contact_mobile'		=> $_POST['contact_mobile_prefix'] .'/'. $_POST['contact_mobile_number'],
		    'contact_fax'		=> $_POST['contact_fax_prefix'] .'/'. $_POST['contact_fax_number'],
		    'contact_city'		=> $_POST['contact_city'],
		    'contact_cap'		=> $_POST['contact_cap'],
		    'contact_loc'		=> $_POST['contact_loc'],
		    'contact_street'		=> $_POST['contact_street'],
		    'contact_street_number'	=> $_POST['contact_street_number'],
		    'contact_province_id'	=> $_POST['contact_province_id'],		    
		    'contact_notes'		=> $_POST['contact_notes'],		    
		    'contact_modifiedby'	=> $this->application->auth->user['user_id']
		);
		    
		$model = new Classes_Contact( $data );

		if( is_numeric( $contact_id = $model->save()->contact_id ) )
		{
		    // Rimanda alla pagina di conferma e cancella file temporanei...
		    $this->layout->forward = 'contatti/rubrica';

		    return $this->layout->content = array( 'script' => 'completed' );
		}
		else
		{
		    trigger_error( 'Errore Edit Contatto' );
		}
	    }
	}

	$this->layout->errorMsg = $errorMsg;
	$this->layout->content = array( 'script' => 'contatti/edit' );	
    }
    
    public function vediAction()
    {
	if( is_null( $this->getParam( 'id' ) ) )
	{
	    trigger_error( 'Errore Id Contatto' );
	}
	
	// Fetch
	$contact	= $this->model_contact->fetchById( array( $this->getParam( 'id' ) ) );

	$this->roles	= $this->application->getResource( 'ContactRoles' );
	
	// Se non c'è il post lancia l'errore
	if( !is_array( $contact ) )
	{
	    trigger_error( 'Errore Id Contatto', E_USER_WARNING );
	}
	else
	{
	    # Assegniamo i dati del post al layout
	    $this->layout->data = array_shift( $contact );
	}
	
	$this->layout->errorMsg = $errorMsg;
	$this->layout->content = array( 'script' => 'contatti/vedi' );		
    }

    public function associaAction()
    {
	// Associa Contatto a Locale
	if( is_null( $this->getParam( 'id' ) ) )
	{
	    trigger_error( 'Errore Associa Contatto' );
	}
	
	// Fetch
	$contact = $this->model_contact->fetchById( array( $this->getParam( 'id' ) ) );

	$this->model_location	= new Classes_Location();
	$this->locations	= $this->model_location->fetchAll();
	
	// Se non c'è il post lancia l'errore
	if( !is_array( $contact ) )
	{
	    trigger_error( 'Errore Associa Contatto', E_USER_WARNING );
	}
	else
	{
	    # Assegniamo i dati del post al layout
	    $this->layout->data = $contact;
	}
	
	if( $_POST['salva'] AND $_POST['salva'] !== '' )
	{
	    $roles = array();
	    
	    foreach( $_POST AS $key => $value )
	    {
		if( substr( $key,0,9 ) == 'location_' )
		{
		    // Mapping...
		    $roles[] = array( 'fab_contacts_contact_id' => $contact['contact_id'], 'fab_locations_location_id' => substr( $key, 9 ), 'fab_contacts_role' => $value );
		}
	    }
	    
	    if( !$errorMsg )
	    {
		$model = new Classes_ContactLocation( array( 'fab_contacts_contact_id' => $contact['contact_id'] ) );
		
		if(  $model->save( $roles ) === TRUE )
		{
		    // Rimanda alla pagina di conferma e cancella file temporanei...
		    $this->layout->forward = 'contatti/archivio';

		    return $this->layout->content = array( 'script' => 'completed' );
		}
		else
		{
		    trigger_error( 'Errore Edit Contatto' );
		}
	    }
	}

	$this->layout->errorMsg	    = $errorMsg;
	$this->layout->locations    = $this->locations;
	$this->layout->roles	    = $this->roles;
	$this->layout->content	    = array( 'script' => 'contatti/associa' );		
    }


    public function rubricaAction()
    {
	$this->layout->contacts = $this->model_contact->fetchAll();
	$this->layout->content = array( 'script' => 'contatti/rubrica' );
    }
    
    private function checkFormData( $data )
    {
	if( !filter_var( $data['contact_firstname'], FILTER_SANITIZE_STRING ) OR !filter_var( $data['contact_lastname'], FILTER_SANITIZE_STRING ) )
	    $errorMsg['contact_name'] = sprintf( ERR_FORMAT, 'Nome o Cognome' );

	if( !filter_var( $data['contact_email'], FILTER_VALIDATE_EMAIL ) )
	    $errorMsg['contact_email'] = ERR_EMAIL;
	
	if( !filter_var( $data['contact_phone_prefix'], FILTER_VALIDATE_REGEXP, array( 'options' => array( 'regexp' => "/^[0-9]*$/" ) ) ) OR !filter_var( $data['contact_phone_number'], FILTER_VALIDATE_REGEXP, array( 'options' => array( 'regexp' => "/[0-9]*/" ) ) ) )
	    $errorMsg['contact_phone'] = sprintf( ERR_FORMAT, 'Tel. Principale' );

	/* Info Opzionali */
	if( $data['contact_email2'] !== '' AND !filter_var( $data['contact_email2'], FILTER_VALIDATE_EMAIL ) )
	    $errorMsg['contact_email2'] = sprintf( ERR_FORMAT, 'Email 2' );

	if( $data['contact_email3'] !== '' AND !filter_var( $data['contact_email3'], FILTER_VALIDATE_EMAIL ) )
	    $errorMsg['contact_email3'] = sprintf( ERR_FORMAT, 'Email 3' );

	if( $data['contact_cap'] !== '' AND ( ( strlen( $data['contact_cap'] ) != 0 AND strlen( $data['contact_cap'] ) != 5 ) OR ( !filter_var( $data['contact_cap'], FILTER_VALIDATE_REGEXP, array( 'options' => array( 'regexp' => "/^[0-9]{5}$/" ) ) ) ) ) )
	    $errorMsg['contact_address'] = sprintf( ERR_FORMAT, 'CAP' );

	if( ( $data['contact_mobile_prefix'] !== '' OR $data['contact_mobile_number'] !== '' ) AND ( !filter_var( $data['contact_mobile_prefix'], FILTER_VALIDATE_REGEXP, array( 'options' => array( 'regexp' => "/^[0-9]*$/" ) ) ) OR !filter_var( $data['contact_mobile_prefix'], FILTER_VALIDATE_REGEXP, array( 'options' => array( 'regexp' => "/[0-9]*/" ) ) ) ) )
	    $errorMsg['contact_mobile'] = sprintf( ERR_FORMAT, 'Tel. Privato' );
	
	if( ( $data['contact_work_prefix'] !== '' OR $data['contact_work_number'] !== '' ) AND ( !filter_var( $data['contact_work_prefix'], FILTER_VALIDATE_REGEXP, array( 'options' => array( 'regexp' => "/^[0-9]*$/" ) ) ) OR !filter_var( $data['contact_work_prefix'], FILTER_VALIDATE_REGEXP, array( 'options' => array( 'regexp' => "/[0-9]*/" ) ) ) ) )
	    $errorMsg['contact_work'] = sprintf( ERR_FORMAT, 'Tel. Lavoro' );
	
	if( ( $data['contact_fax_prefix'] !== '' OR $data['contact_fax_number'] !== '' ) AND ( !filter_var( $data['contact_fax_prefix'], FILTER_VALIDATE_REGEXP, array( 'options' => array( 'regexp' => "/^[0-9]*$/" ) ) ) OR !filter_var( $data['contact_fax_number'], FILTER_VALIDATE_REGEXP, array( 'options' => array( 'regexp' => "/[0-9]*/" ) ) ) ) )
	    $errorMsg['contact_fax'] = sprintf( ERR_FORMAT, 'Fax' );	
	
	return $errorMsg ? $errorMsg : false;
    }
} 

